package tds.model10.version2;

import org.apache.commons.lang3.SerializationUtils;

/**
 * @Company 杭州先锋电子技术股份有限公司
 * @ClassName Client
 * @Description  Client（客户类）：让一个原型对象克隆自身从而创建一个新的对象.
 * @author tds
 * @Date 2019/4/25 10:48
 * @version 1.0.0
 */
public class Client {
    public static void main(String[] args) {
        //浅拷贝
        ConcretePrototype concretePrototype = new ConcretePrototype();
        concretePrototype.setAttr("ccc");
        concretePrototype.setObj(new SerialObject());
        ConcretePrototype clonePrototype = concretePrototype.clone();
        clonePrototype.setAttr("bbb");
        System.out.println(concretePrototype == clonePrototype);
        System.out.println(concretePrototype);
        System.out.println(clonePrototype);
        System.out.println("浅拷贝的obj对比：" + (concretePrototype.getObj() == clonePrototype.getObj()));//==的优先级低于+

        //深拷贝
        ConcretePrototype deepClonePrototype = concretePrototype.deepClone();
        System.out.println("深拷贝的结果：" + deepClonePrototype);
        System.out.println("深拷贝的obj对比：" + (deepClonePrototype.getObj() == concretePrototype.getObj()));

        //使用SerializationUtils实现深拷贝
        SerialObject serialObject = new SerialObject();
        SerialObject cloneObj = SerializationUtils.clone(serialObject);
        System.out.println(serialObject == cloneObj);
    }
}
